package com.sc.byahi.user.client.components;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class CatSelectionList extends Composite {
	final ListBox options = new ListBox();
	final ListBox selected = new ListBox();
	final VerticalPanel v = new VerticalPanel();
	final HorizontalPanel h = new HorizontalPanel();
	final VerticalPanel v1 = new VerticalPanel();
	final VerticalPanel v2 = new VerticalPanel();

	/**
	 * @wbp.parser.constructor
	 */
	public CatSelectionList() {
		FlexTable tab = new FlexTable();
		tab.setStyleName("my-form");
		tab.setStylePrimaryName("my-form");
		initWidget(tab);
		setStyleName(".my-form");

		tab.setWidth("250px");
		tab.setWidget(0, 0, options);
		tab.setWidget(0, 1, selected);

		options.setWidth("150px");
		selected.setWidth("150px");
		selected.setVisibleItemCount(5);
		options.setVisibleItemCount(5);

		options.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				int index = options.getSelectedIndex();
				if (index != -1) {
					String itemText = options.getItemText(index);
					options.removeItem(index);
					optionsData.remove(itemText);
					selectedData.add(itemText);
					Collections.sort(selectedData);
					selected.insertItem(itemText,
							selectedData.indexOf(itemText));
				}
			}
		});
		selected.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				int index = selected.getSelectedIndex();
				if (index != -1) {
					String itemText = selected.getItemText(index);
					selected.removeItem(index);
					selectedData.remove(itemText);
					optionsData.add(itemText);
					Collections.sort(optionsData);
					options.insertItem(itemText, optionsData.indexOf(itemText));
				}
			}
		});

	}

	private void sort() {
		Collections.sort(optionsData);
		Collections.sort(selectedData);
	}

	private List<String> optionsData = new ArrayList<String>();
	private List<String> selectedData = new ArrayList<String>();

	private boolean optionsSet = false;
	private boolean selectedSet = false;

	public void setOptionsData(List<String> optionsData) {
		this.optionsData.clear();
		this.options.clear();
		this.optionsData.addAll(optionsData);
		for (String string : this.optionsData) {
			options.addItem(string);
		}
		optionsSet = true;
	}

	public void setSelectedData(List<String> selectedData) {
		this.selectedData.clear();
		this.selected.clear();
		this.selectedData.addAll(selectedData);
		for (String string : this.selectedData) {
			selected.addItem(string);
			if (optionsSet) {
				options.removeItem(optionsData.indexOf(string));
				optionsData.remove(string);
			}
		}
		selectedSet = true;
	}

	public List<String> getSelectedItems() {
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < selected.getItemCount(); i++) {
			String str = selected.getItemText(i);
			list.add(str);
		}
		return list;
	}

}
